package top.ligongzi.hrm.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import top.ligongzi.hrm.contstants.HrmResourceConstants;

//资源服务配置
@Configuration
//开启资源服务配置
@EnableResourceServer
//开启方法授权
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class BaseResourceServerConfig extends ResourceServerConfigurerAdapter {
    //转换器
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){

        //创建
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        //设置秘钥
        jwtAccessTokenConverter.setSigningKey(HrmResourceConstants.JWT_SIGNING_KEY);
        return jwtAccessTokenConverter;
    }
    //构造jwttokenstore
    @Bean
    public TokenStore tokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Value("${oauth2.resourceId}") //从配置文件中获取值
    private String resourceId;
    //1.资源服务安全配置
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        //配置资源id，对应oauth_client_details表中resource_ids
        resources.resourceId(resourceId);
        //远程发请求去获取token中用户和权限
        resources.tokenStore(tokenStore());
    }

    public String getResourceId() {
        return resourceId;
    }

    public void setResourceId(String resourceId) {
        this.resourceId = resourceId;
    }

}